package cn.com.Servlet.AdminServlet;
import cn.com.controler.Admin.AdminDAO;
import cn.com.controler.AdminArticle.AdminArticleDAO;
import cn.com.controler.Article.ArticleDAO;
import cn.com.controler.Comment.CommentDAO;
import cn.com.controler.CommentView.CommentViewDAO;
import cn.com.controler.User.UserDAO;
import cn.com.controler.Vipapplication.VipapplicationDAO;
import cn.com.entity.*;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "AdminLoginServlet",urlPatterns = "/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        AdminDAO adminDAO = new AdminDAO();
        ArticleDAO articleDAO = new ArticleDAO();
        CommentDAO commentDAO = new CommentDAO();
        if(type.equals("adminlogin")){       //管理员用户名登录
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String admintype = request.getParameter("admintype");
            HttpSession session = request.getSession();
            if(admintype.equals("pg")){   //普管登录
                List<Admin> loginselect = adminDAO.loginselect(username, password);
                if(loginselect.size() != 0){
                    Admin admin = loginselect.get(0);
                    session.setAttribute("admin",admin);
                    response.sendRedirect("/WebContent/AdminFrame/Adminmain.jsp");
                }else{
                    response.sendRedirect("/WebContent/HTML/AdminLogin.jsp");
                }
            }else if(admintype.equals("cg")){   //超管登录
                List<Admin> loginselect = adminDAO.cjloginselect(username, password);
                if(loginselect.size() != 0){
                    Admin admin = loginselect.get(0);
                    session.setAttribute("admin",admin);
                    response.sendRedirect("/WebContent/SuperAdminFrame/Adminmain.jsp");
                }else{
                    response.sendRedirect("/WebContent/HTML/AdminLogin.jsp");
                }
            }
        }else if(type.equals("adduser")){        //管理员手动添加用户
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String year = request.getParameter("year");
            String month = request.getParameter("month");
            String day = request.getParameter("day");
            String tel = request.getParameter("tel");
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String resitime = format.format(date);  //注册时间
            String birth = year+"-"+month+"-"+day;
            UserDAO insertDAO = new UserDAO();
            User use = new User(null,username,password,birth,tel,email,resitime,0);
            int adduser = insertDAO.adduser(use);
            if(adduser > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("upadmin")){        //修改管理员信息
            Integer id = Integer.valueOf(request.getParameter("id"));
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            int i = adminDAO.updateupe(id, username, password, email);
            if(i > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("artdel")){        //管理员删除帖子
            Integer id = Integer.valueOf(request.getParameter("id"));
            int i = articleDAO.delarticleby(id);
            if(i > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("commdel")){        //管理员删除评论
            Integer id = Integer.valueOf(request.getParameter("id"));
            int i = commentDAO.deletebyid(id);
            if(i > 0){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        UserDAO userDAO = new UserDAO();
        AdminDAO adminDAO = new AdminDAO();
        AdminArticleDAO adminArticleDAO = new AdminArticleDAO();
        VipapplicationDAO vipapplicationDAO = new VipapplicationDAO();
        CommentViewDAO commentViewDAO = new CommentViewDAO();
        if(type.equals("tc")){     //管理员退出系统
            HttpSession session = request.getSession();
            session.invalidate();
            response.getWriter().write("1");
        }else if(type.equals("cgloginstate")){
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            int getroletypestate = adminDAO.getroletypestate(id);
            if(getroletypestate == 1){
                response.getWriter().write("1");
            }else{
                response.getWriter().write("0");
            }
        }else if(type.equals("alladmin")){           //查询所有管理员
            List<Admin> selectall = adminDAO.selectall();
            request.setAttribute("admins",selectall);
            request.getRequestDispatcher("/WebContent/HTML/AdminPage/admin_admin/ShowAdmin.jsp").forward(request,response);
        }else if(type.equals("allusers")) {        //查询所有用户
            List<User> selectalluser = userDAO.selectalluser();
            request.setAttribute("users", selectalluser);
            request.getRequestDispatcher("/WebContent/HTML/AdminPage/admin_user/Glusers.jsp").forward(request, response);
        }else if(type.equals("cxviptypes")){    //查询所有未处理申请
            //查询所有
            List<Vipapplication> allsqwcl = vipapplicationDAO.allsqwcl();
            JSONArray jsonArray = JSONArray.fromObject(allsqwcl);
            PrintWriter writer = response.getWriter();
            writer.write(jsonArray.toString());
            writer.flush();
            writer.close();
        }else if(type.equals("cxmydata")){    //查询个人信息
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            List<Admin> selectbyid = adminDAO.selectbyid(id);
            JSONArray jsonArray = JSONArray.fromObject(selectbyid);
            PrintWriter writer = response.getWriter();
            writer.write(jsonArray.toString());
            writer.flush();
            writer.close();
        }else if(type.equals("agree")){    //管理员同意vip申请
            String id1 = request.getParameter("id");    //vip申请id
            Integer id = Integer.valueOf(id1);
            String id2 = request.getParameter("userid");   //用户id，外键指向user中的id
            Integer idd = Integer.valueOf(id2);
            PrintWriter writer = response.getWriter();     //获取输出流对象
            int agreeapp = vipapplicationDAO.agreeapp(id);
            if(agreeapp > 0){
                //修改user表中该用户的状态
                int i = userDAO.updatestateagree(idd);
                if(i > 0){
                    writer.write("1");
                    writer.flush();
                    writer.close();
                }else{
                    writer.write("0");
                    writer.flush();
                    writer.close();
                }
            }else{
                writer.write("0");
                writer.flush();
                writer.close();
            }
        }else if(type.equals("refuse")){    //管理员拒绝vip申请
            String id1 = request.getParameter("id");
            Integer id = Integer.valueOf(id1);
            String id2 = request.getParameter("userid");
            Integer idd = Integer.valueOf(id2);
            String username = request.getParameter("username");
            PrintWriter writer = response.getWriter();
            int agreeapp = vipapplicationDAO.refuseapp(id);
            if(agreeapp > 0){
                int i = userDAO.updatestaterefuse(idd);
                if(i > 0){
                    writer.write("1");
                    writer.flush();
                    writer.close();
                }else{
                    writer.write("0");
                    writer.flush();
                    writer.close();
                }
            }else{
                writer.write("0");
                writer.flush();
                writer.close();
            }
        }else if(type.equals("glarticle")){    //管理员管理帖子
            List<AdminArticle> all = adminArticleDAO.getAll();
            request.setAttribute("glart",all);
            request.getRequestDispatcher("/WebContent/HTML/AdminPage/admin_article/GlArticle.jsp").forward(request,response);
        }else if(type.equals("glcomment")){    //管理员管理评论
            List<CommentView> all = commentViewDAO.getAll();
            request.setAttribute("glcomm",all);
            request.getRequestDispatcher("/WebContent/HTML/AdminPage/admin_comment/Glpinglun.jsp").forward(request,response);
        }
    }
}
